define(['angular'], function (angular) {

/*-----------------------------------------
 *   demo serice
 ------------------------------------------*/
angular.module('books.service.demo', [])
    .constant('demoServiceConfig', {
        files:{//调用json数据
            retrTableList:'app/data/demo/retrTableList.json',
            saveTableList:'app/data/demo/savetableList.json',
            removeTableList:'app/data/demo/removeTableList.json',
            submitFormData:'app/data/demo/submitFormData.json'
        },
        urls:{
            retrTableList:'xx/retrTableList.do',//调用后台api接口
            saveTableList:'xx/saveTableList.do',
            removeTableList:'xx/removeTableList.do',
            submitFormData:'xx/saveTableList.do'
        }
    })
    .factory('demoService',['$http', '$q', 'demoServiceConfig', function ($http, $q, demoServiceConfig) {
        return {
            /**
             * 查询tableList 列表
             * 
             * @return {[type]} [description]
             */
            retrTableList: function(){
                //内置服务 同步请求数据
                var deffered = $q.defer();
                //请求数据
                $http({
                    method:'GET',
                    dataType: "json",
                    contentType:'application/json; charset=UTF-8',
                    url:demoServiceConfig.files.retrTableList
                })
                .success(function(response){
                    deffered.resolve(response);
                })
                .error(function(e, code){
                    deffered.reject(code);
                });
                return deffered.promise; 
            },

            /**
             * 保存grid修改
             * 
             * @return {[type]} [description]
             */
            saveTableList: function(saveJson){
                //内置服务 同步请求数据
                var deffered = $q.defer();
                //请求数据
                $http({
                    method:'GET',
                    dataType: "json",
                    contentType:'application/json; charset=UTF-8',
                    url:demoServiceConfig.files.saveTableList,
                    params:{
                        saveJson: saveJson
                    }
                })
                .success(function(response){
                    deffered.resolve(response);
                })
                .error(function(e, code){
                    deffered.reject(code);
                });
                return deffered.promise;    
            },

            /**
             * 删除选中项
             * 
             * @return {[type]} [description]
             */
            removeTableList: function(bookId){
                //内置服务 同步请求数据
                var deffered = $q.defer();
                //请求数据
                $http({
                    method:'GET',
                    dataType: "json",
                    contentType:'application/json; charset=UTF-8',
                    url:demoServiceConfig.files.removeTableList,
                    params:{
                        bookId: bookId
                    }
                })
                .success(function(response){
                    deffered.resolve(response);
                })
                .error(function(e, code){
                    deffered.reject(code);
                });
                return deffered.promise;    
            },

             /**
             * 提交formPane数据
             * 
             * @return {[type]} [description]
             */
            submitFormData: function(){
                //内置服务 同步请求数据
                var deffered = $q.defer();
                //请求数据
                $http({
                    // method:'GET',
                    dataType: "json",
                    contentType:'application/json; charset=UTF-8',
                    url:demoServiceConfig.files.submitFormData
                })
                .success(function(response){
                    deffered.resolve(response);
                })
                .error(function(e, code){
                    deffered.reject(code);
                });
                return deffered.promise;    
            },

            /**
             * 根据系统代码类型（sys_code），返回代码值、代码名，并按顺序号排序 
             * 一般用于查询下拉框的公共方法~ 暂时没用
             * 
             * @param codeType
             * @param showID
             * @return
             */
            retrSysCodes:function(codeType,showID){
                var deffered = $q.defer();//内置服务 同步请求数据
                //请求数据
                $http({
                    // method:'POST',
                    dataType: "json",
                    contentType:'application/json; charset=UTF-8',
                    url:systemServiceConfig.urls.retrSysCodes,
                    params:{
                        codeType : codeType,
                        showID : showID
                    }
                })
                .success(function(response){
                    deffered.resolve(response);
                })
                .error(function(e, code){
                    deffered.reject(code);
                });
                return deffered.promise;    
            }
            
        };
    }]);

});
